package logic

import (
	"context"
	"errors"
	"storage/common"

	"storage/internal/svc"
	"storage/pb/storage"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetFolderListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetFolderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFolderListLogic {
	return &GetFolderListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetFolderList 获取文件夹列表
func (l *GetFolderListLogic) GetFolderList(in *storage.FolderListRequest) (*storage.FolderListResponse, error) {
	var orderByStr string
	orderByStr, ok := common.FolderOrderMap[int(in.OrderBy)]
	if !ok {
		return nil, errors.New("orderBy参数错误")
	}
	folders, total, lastId, err := l.svcCtx.StorageModel.GetFolderList(l.ctx, in.UserId, in.ParentId, in.Status, in.LastId, in.Limit, in.Keyword, orderByStr)
	if err != nil {
		return nil, err
	}

	var list []*storage.FolderInfo
	for _, folder := range folders {
		list = append(list, &storage.FolderInfo{
			Id:         folder.Id,
			UserId:     folder.UserId,
			ParentId:   folder.ParentId,
			FolderName: folder.FolderName,
			Status:     folder.Status,
			CreateAt:   folder.CreateAt.Unix(),
			UpdateAt:   folder.UpdateAt.Unix(),
		})
	}
	return &storage.FolderListResponse{Total: total, LastId: lastId, List: list}, nil
}
